package com.badger.pattern;

import java.util.ArrayList;
import java.util.List;
/**
 * 观察者模式
 * @author liqi
 *
 */
public class TestObserver {

    public static void main(String[] args) {
        // 创建主题
        Subject subject = new Subjectable();
        // 注册消费者
        buildObServer(1, subject);
        buildObServer(2, subject);
        buildObServer(3, subject);
        // 给观察者发送消息
        for (int i = 0; i < 5; i++) {
            System.out.println("开始打招呼！");
            String dd = String.format("第%s次，大家好！", i);
            subject.notifyMessage(dd);
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    static ObServerAnimal buildObServer(int i, Subject subject) {
        ObServerAnimal obs;
        switch (i) {
        case 1:
            obs = new Dog(subject);
            break;
        case 2:
            obs = new Elephant(subject);
            break;
        case 3:
            obs = new Cat(subject);
            break;
        default:
            obs = new Dog(subject);
            break;
        }
        return obs;
    }

    /**主题*/
    interface Subject {
        /**观察者注册*/
        void register(ObServerAnimal obs);

        /**观察者删除*/
        void remove(ObServerAnimal obs);

        /**发送通知给观察者*/
        void notifyMessage(String message);
    }

    static class Subjectable implements Subject {
        static List<ObServerAnimal> list = new ArrayList<>();

        @Override
        public void register(ObServerAnimal obs) {
            list.add(obs);
        }

        @Override
        public void remove(ObServerAnimal obs) {
            final int i = list.indexOf(obs);
            if (i > -1) {
                list.remove(i);
            }
        }

        @Override
        public void notifyMessage(final String message) {
            list.stream().forEach(obs -> obs.getMessage(message));
        }

    }

    /**观察者*/
    interface ObServerAnimal {
        String getMessage(String message);
    }

    static class Dog implements ObServerAnimal {

        public Dog(Subject subject) {
            subject.register(this);
        }

        @Override
        public String getMessage(String message) {
            System.out.println(String.format("this is dog ,The message is %s", message));
            return message;
        }

    }

    static class Elephant implements ObServerAnimal {

        public Elephant(Subject subject) {
            subject.register(this);
        }

        @Override
        public String getMessage(String message) {
            System.out.println(String.format("this is Elephant ,The message is %s", message));
            return message;
        }

    }

    static class Cat implements ObServerAnimal {
        public Cat(Subject subject) {
            subject.register(this);
        }

        @Override
        public String getMessage(String message) {
            System.out.println(String.format("this is Cat ,The message is %s", message));
            return message;
        }

    }
}
